﻿#include "CSensorSeriesList.h"
#include "CDevDataSensorSeries.h"

CSensorSeriesList::CSensorSeriesList(CDevSensorSeries* pDev,QWidget *parent)
	:m_pDev(pDev), QWidget(parent)
{
	ui.setupUi(this);
	m_sNormalLamp = "min-width:20px;min-height:20px;"
		"max-width:20px;max-height:20px;"
		"border-radius:10px;border:1px solid black;"
		"background:rgb(120, 120, 120);";
	m_sWorkLamp = "min-width:20px;min-height:20px;"
		"max-width:20px;max-height:20px;"
		"border-radius:10px;border:1px solid black;"
		"background:rgb(0, 255, 0);";
	m_sAlarmLamp = "min-width:20px;min-height:20px;"
		"max-width:20px;max-height:20px;"
		"border-radius:10px;border:1px solid black;"
		"background:rgb(255, 0, 0);";
	m_sWarningLamp = "min-width:20px;min-height:20px;"
		"max-width:20px;max-height:20px;"
		"border-radius:10px;border:1px solid black;"
		"background:rgb(255,255,0);";
	//setStyleSheetFile();
	m_pDevData = (CDevDataSensorSeries*)m_pDev->m_pDevData;
	initConnect();
	setName();
	//配置定时器
	m_pTimer = new QTimer(this);
	m_pTimer->setTimerType(Qt::TimerType::PreciseTimer);
	connect(m_pTimer, &QTimer::timeout, this, &CSensorSeriesList::slotDataRefresh);
	m_pTimer->start(5000);//5秒的定时器
	slotDataRefresh();
}

CSensorSeriesList::~CSensorSeriesList()
{
	m_pTimer->stop();
	int nIndex = 0;
	while (m_pTimer->isActive())
	{
		if (nIndex > 500)
		{
			break;
		}
		nIndex++;
		QThread::msleep(10);
	}
}

void CSensorSeriesList::setStyleSheetFile()
{
	this->setStyleSheet("QGroupBox{border: 1px solid black;margin-top: 2.3ex;border-radius: 10px;background-color: #888888;}"
		"QGroupBox::title{padding: 0 3px; font-size: 16px;subcontrol- origin:margin;left: 10px;}"
		"QLabel{background-color: transparent;border: 1px solid transparent;}"
	);
}

void CSensorSeriesList::initConnect()
{
	connect(ui.pushButtonChannel1, &QPushButton::clicked, this, &CSensorSeriesList::slotPushBtnChannelClicked);
	connect(ui.pushButtonChannel2, &QPushButton::clicked, this, &CSensorSeriesList::slotPushBtnChannelClicked);
	connect(ui.pushButtonChannel3, &QPushButton::clicked, this, &CSensorSeriesList::slotPushBtnChannelClicked);
	connect(ui.pushButtonChannel4, &QPushButton::clicked, this, &CSensorSeriesList::slotPushBtnChannelClicked);
	connect(ui.pushButtonChannel5, &QPushButton::clicked, this, &CSensorSeriesList::slotPushBtnChannelClicked);
	connect(ui.pushButtonChannel6, &QPushButton::clicked, this, &CSensorSeriesList::slotPushBtnChannelClicked);
	connect(ui.pushButtonChannel7, &QPushButton::clicked, this, &CSensorSeriesList::slotPushBtnChannelClicked);
	connect(ui.pushButtonChannel8, &QPushButton::clicked, this, &CSensorSeriesList::slotPushBtnChannelClicked);
	connect(m_pDevData, &CDevDataSensorSeries::sigCfgChanged, this, &CSensorSeriesList::slotCfgChannged);
}

void CSensorSeriesList::setName()
{
	ui.label->setText(m_pDevData->m_sDevName);
	ui.pushButtonChannel1->setText(QString::fromLocal8Bit("通道1-%1").arg(m_pDevData->m_mapChannel[1].sChannelName));
	ui.pushButtonChannel1->setProperty("Guid", m_pDevData->m_sDevGuid +"-"+ QString::number(1));
	ui.pushButtonChannel2->setText(QString::fromLocal8Bit("通道2-%1").arg(m_pDevData->m_mapChannel[2].sChannelName));
	ui.pushButtonChannel2->setProperty("Guid", m_pDevData->m_sDevGuid + "-" + QString::number(2));
	ui.pushButtonChannel3->setText(QString::fromLocal8Bit("通道3-%1").arg(m_pDevData->m_mapChannel[3].sChannelName));
	ui.pushButtonChannel3->setProperty("Guid", m_pDevData->m_sDevGuid + "-" + QString::number(3));
	ui.pushButtonChannel4->setText(QString::fromLocal8Bit("通道4-%1").arg(m_pDevData->m_mapChannel[4].sChannelName));
	ui.pushButtonChannel4->setProperty("Guid", m_pDevData->m_sDevGuid + "-" + QString::number(4));
	ui.pushButtonChannel5->setText(QString::fromLocal8Bit("通道5-%1").arg(m_pDevData->m_mapChannel[5].sChannelName));
	ui.pushButtonChannel5->setProperty("Guid", m_pDevData->m_sDevGuid + "-" + QString::number(5));
	ui.pushButtonChannel6->setText(QString::fromLocal8Bit("通道6-%1").arg(m_pDevData->m_mapChannel[6].sChannelName));
	ui.pushButtonChannel6->setProperty("Guid", m_pDevData->m_sDevGuid + "-" + QString::number(6));
	ui.pushButtonChannel7->setText(QString::fromLocal8Bit("通道7-%1").arg(m_pDevData->m_mapChannel[7].sChannelName));
	ui.pushButtonChannel7->setProperty("Guid", m_pDevData->m_sDevGuid + "-" + QString::number(7));
	ui.pushButtonChannel8->setText(QString::fromLocal8Bit("通道8-%1").arg(m_pDevData->m_mapChannel[8].sChannelName));
	ui.pushButtonChannel8->setProperty("Guid", m_pDevData->m_sDevGuid + "-" + QString::number(8));
}

void CSensorSeriesList::setState(int nSensorSeriesState, int nChannel1, int nChannel2, int nChannel3, int nChannel4, int nChannel5, int nChannel6, int nChannel7, int nChannel8)
{
	if (nSensorSeriesState == 0)
	{
		ui.labelSensorSeriesState->setStyleSheet(m_sNormalLamp);
	}
	else if (nSensorSeriesState == 1)
	{
		ui.labelSensorSeriesState->setStyleSheet(m_sWorkLamp);
	}

	if (nChannel1 == 0)
	{
		ui.labelChannel1->setStyleSheet(m_sNormalLamp);
		ui.pushButtonChannel1->setEnabled(false);
	}
	else if (nChannel1 == 1)
	{
		ui.labelChannel1->setStyleSheet(m_sWorkLamp);
		ui.pushButtonChannel1->setEnabled(true);
	}

	if (nChannel2 == 0)
	{
		ui.labelChannel2->setStyleSheet(m_sNormalLamp);
		ui.pushButtonChannel2->setEnabled(false);
	}
	else if (nChannel2 == 1)
	{
		ui.labelChannel2->setStyleSheet(m_sWorkLamp);
		ui.pushButtonChannel2->setEnabled(true);
	}

	if (nChannel3 == 0)
	{
		ui.labelChannel3->setStyleSheet(m_sNormalLamp);
		ui.pushButtonChannel3->setEnabled(false);
	}
	else if (nChannel3 == 1)
	{
		ui.labelChannel3->setStyleSheet(m_sWorkLamp);
		ui.pushButtonChannel3->setEnabled(true);
	}

	if (nChannel4 == 0)
	{
		ui.labelChannel4->setStyleSheet(m_sNormalLamp);
		ui.pushButtonChannel4->setEnabled(false);
	}
	else if (nChannel4 == 1)
	{
		ui.labelChannel4->setStyleSheet(m_sWorkLamp);
		ui.pushButtonChannel4->setEnabled(true);
	}

	if (nChannel5 == 0)
	{
		ui.labelChannel5->setStyleSheet(m_sNormalLamp);
		ui.pushButtonChannel5->setEnabled(false);
	}
	else if (nChannel5 == 1)
	{
		ui.labelChannel5->setStyleSheet(m_sWorkLamp);
		ui.pushButtonChannel5->setEnabled(true);
	}

	if (nChannel6 == 0)
	{
		ui.labelChannel6->setStyleSheet(m_sNormalLamp);
		ui.pushButtonChannel6->setEnabled(false);
	}
	else if (nChannel6 == 1)
	{
		ui.labelChannel6->setStyleSheet(m_sWorkLamp);
		ui.pushButtonChannel6->setEnabled(true);
	}

	if (nChannel7 == 0)
	{
		ui.labelChannel7->setStyleSheet(m_sNormalLamp);
		ui.pushButtonChannel7->setEnabled(false);
	}
	else if (nChannel7 == 1)
	{
		ui.labelChannel7->setStyleSheet(m_sWorkLamp);
		ui.pushButtonChannel7->setEnabled(true);
	}

	if (nChannel8 == 0)
	{
		ui.labelChannel8->setStyleSheet(m_sNormalLamp);
		ui.pushButtonChannel8->setEnabled(false);
	}
	else if (nChannel8 == 1)
	{
		ui.labelChannel8->setStyleSheet(m_sWorkLamp);
		ui.pushButtonChannel8->setEnabled(true);
	}
}

void CSensorSeriesList::slotDataRefresh()
{
	if (isVisible())
	{
		int nSensorSeries = m_pDevData->m_nState;
		int nChannel1 = m_pDevData->m_mapChannel[1].nState;
		int nChannel2 = m_pDevData->m_mapChannel[2].nState;
		int nChannel3 = m_pDevData->m_mapChannel[3].nState;
		int nChannel4 = m_pDevData->m_mapChannel[4].nState;
		int nChannel5 = m_pDevData->m_mapChannel[5].nState;
		int nChannel6 = m_pDevData->m_mapChannel[6].nState;
		int nChannel7 = m_pDevData->m_mapChannel[7].nState;
		int nChannel8 = m_pDevData->m_mapChannel[8].nState;
		setState(nSensorSeries, nChannel1, nChannel2, nChannel3, nChannel4, nChannel5, nChannel6, nChannel7, nChannel8);
	}
}

void CSensorSeriesList::slotPushBtnChannelClicked()
{
	QPushButton *btn = (QPushButton *)sender();
	QString sGuid = btn->property("Guid").toString();//通道号Guid
	emit(sigChannelClicked(sGuid));
}

void CSensorSeriesList::slotCfgChannged()
{
	setName();
}
